<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_kasus extends CI_Model {

    var $kasus_id;
    var $kasus_tanggal;
    var $kasus_kat_kasus;
    var $kasus_sub_kat_kasus;
    var $kasus_santri;
    var $kasus_keterangan;
    var $kasus_oleh;
    var $kasus_dirubah;
    var $cari;
    var $paginate;
    var $num_pages;
    var $offset;

    function __construct() {
        parent::__construct();
        $this->load->library('auth');
        $this->load->helper('date');
        $this->load->model(array('m_santri', 'm_kat_kasus', 'm_sub_kasus'));
    }

    public function get_row_all() {
        if ($this->cari) {
            $this->db->where('(kasus_keterangan LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR kasus_keterangan LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        $this->db->order_by('kasus_id', 'DESC');
        $query = $this->db->count_all_results('t_kasus');

        return $query;
    }

    public function get() {
        if ($this->cari) {
            $this->db->where('(kasus_keterangan LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR kasus_keterangan LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        if ($this->paginate == TRUE) {
            $this->db->order_by('kasus_id', 'DESC');
            $query = $this->db->get('t_kasus', $this->num_pages, $this->offset);
            return $query;
        }

        $this->db->order_by('kasus_id', 'DESC');
        $query = $this->db->get('t_kasus');
        return $query;
    }

    public function get_by_id($id = '0') {
        $this->db->where('kasus_id', $id);
        $query = $this->db->get('t_kasus');

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return redirect('homes/');
        }
    }

    function get_kasus_by_santri_id($id = '0') {
        $this->db->where('kasus_santri', $id);
        $this->db->order_by('kasus_id', 'DESC');
        $query = $this->db->get('t_kasus');

        $hasil = array();
        if ($query->num_rows() > 0) {
            //$hasil = $query->result_array();

            $no = 1;
            // Loop through all rows
            foreach ($query->result() as $row) {
                $hasil[] = array(
                    'kasus_id' => $no,
                    // 'kasus_id' => $row->kasus_id,
                    'kasus_santri' => $this->m_santri->get_by_id($row->kasus_santri)->santri_nama,
                    'kasus_tanggal' => $row->kasus_tanggal,
                    'kasus_kat_kasus' => $this->m_kat_kasus->get_by_id($row->kasus_kat_kasus)->kat_kasus_nama,
                    'kasus_sub_kat_kasus' => $this->m_sub_kasus->get_by_id($row->kasus_sub_kat_kasus)->sub_kasus_nama,
                    'kasus_keterangan' => $row->kasus_keterangan,
                    'kasus_oleh' => $row->kasus_oleh,
                        //'kasus_dirubah' => $row->kasus_dirubah
                );
                $no++;
            }
        }

        return $hasil;
    }

    function get_kasus_by_santri_tgl($mulai, $selesai) {
        $mulai = date('Y/m/d', strtotime($mulai));
        $selesai = date('Y/m/d', strtotime($selesai));
        $this->db->where('kasus_tanggal BETWEEN "' . $mulai . '" and "' . $selesai . '"');
        $this->db->order_by('kasus_id', 'DESC');
        $query = $this->db->get('t_kasus');

        $hasil = array();
        if ($query->num_rows() > 0) {
            //$hasil = $query->result_array();

            $no = 1;
            // Loop through all rows
            foreach ($query->result() as $row) {
                $hasil[] = array(
                    'kasus_id' => $no,
                    // 'kasus_id' => $row->kasus_id,
                    'kasus_santri' => $this->m_santri->get_by_id($row->kasus_santri)->santri_nama,
                    'kasus_tanggal' => $row->kasus_tanggal,
                    'kasus_kat_kasus' => $this->m_kat_kasus->get_by_id($row->kasus_kat_kasus)->kat_kasus_nama,
                    'kasus_sub_kat_kasus' => $this->m_sub_kasus->get_by_id($row->kasus_sub_kat_kasus)->sub_kasus_nama,
                    'kasus_keterangan' => $row->kasus_keterangan,
                    'kasus_oleh' => $row->kasus_oleh,
                        //'kasus_dirubah' => $row->kasus_dirubah
                );
                $no++;
            }
        }
        return $hasil;
    }

    public function save() {
        if ($this->kasus_keterangan) {
            $db_array['kasus_keterangan'] = $this->kasus_keterangan;
        }

        if ($this->kasus_sub_kat_kasus) {
            $db_array['kasus_sub_kat_kasus'] = $this->kasus_sub_kat_kasus;
        }

        if ($this->kasus_kat_kasus) {
            $db_array['kasus_kat_kasus'] = $this->kasus_kat_kasus;
        }

        if ($this->kasus_santri) {
            $db_array['kasus_santri'] = $this->kasus_santri;
        }

        if ($this->kasus_tanggal) {
            $db_array['kasus_tanggal'] = $this->kasus_tanggal;
        }

        if ($this->kasus_oleh) {
            $db_array['kasus_oleh'] = $this->kasus_oleh;
        }

        if ($this->kasus_dirubah) {
            $db_array['kasus_dirubah'] = $this->kasus_dirubah;
        }

        if ($this->kasus_id) {
            $this->db->where('kasus_id', $this->kasus_id);
            $this->db->update('t_kasus', $db_array);
        } else {
            $this->db->insert('t_kasus', $db_array);
        }
    }

    public function delete() {
        $this->db->where('kasus_id', $this->kasus_id);
        $this->db->delete('t_kasus');
    }

}

/* End of file m_kasus.php */
/* Location: ./application/model/m_kasus.php */